package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.enums.ResultEnums;
import com.qf.exception.NewsException;
import com.qf.pojo.NewsComment;
import com.qf.pojo.NewsDetail;
import com.qf.service.CommentService;
import com.qf.service.DetailService;
import com.qf.vo.ResultVo;
import com.qf.vo.ResultVoUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Controller
@RequestMapping("/news")
public class DetailController {
    @Autowired
    DetailService detailService;

    @Autowired
    CommentService commentService;

    @GetMapping("/index")
    public String findAll(@RequestParam(value = "page" ,defaultValue = "1")Integer page,
                          @RequestParam(value = "size" , defaultValue = "5")Integer size,
                          String title,
                          Model model){
        PageInfo<NewsDetail> pageinfo = detailService.pageinfo(page, size,title);
        model.addAttribute("pageInfo", pageinfo);
        model.addAttribute("title", title);
        return "index";
    }

    @GetMapping("/viewCommons")
    public String viewCommons(Model model){
       List<NewsComment> commentList= commentService.findAll();
       model.addAttribute("commentList", commentList);
       return "comment";
    }

    @GetMapping("/addcomment-ui")
    public String addcomment(Integer id,Model model){

        model.addAttribute("id", id);
        return "addcomment";
    }

    @PostMapping("/addComment")
    @ResponseBody
    public ResultVo addComment(@Valid NewsComment newsComment, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();
            throw new NewsException(ResultEnums.PARAM_ERRPR.getCode(),message);
        }

        Integer count=commentService.addComment(newsComment);
        if (count!=1){
            throw new NewsException(ResultEnums.PARAM_ERRPR.getCode(),"保存失败");
        }

        return ResultVoUtils.success();

    }

    /**
     *
     * @param id
     * @return
     */

    @GetMapping("/delete")
    public String delete(Integer id){
        Integer count =detailService.deleteById(id);
        if (count!=1){
            throw new NewsException(ResultEnums.PARAM_ERRPR.getCode(),"保存失败");
        }

        return "redirect:/news/index";
    }

    @GetMapping("/tologin")
    public String tologin(){

        return "login";
    }

    @PostMapping("/login")
    public String login(String username,String password,Model model){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(username,password );

        try {
            subject.login(token);
            return "redirect:/news/index";

        } catch (UnknownAccountException e) {
            model.addAttribute("msg","用户名不存在" );
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg", "密码错误");
            return "login";
        }

    }




}
